package br.caixa2.dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import br.caixa2.entities.Client;

public class ClientDao {
	public static void createAll( List<Client> clients ) {
		PrintStream p = null;
		try {
			p = new PrintStream( new File( "client.db" ) );
			for ( Client c : clients ) {
				p.println( c.getName() + "," + c.getCpf() + "," + c.getPhone() + "," + c.getAdress() + " ," );
			}

		} catch ( FileNotFoundException e ) {
			e.printStackTrace();
		} finally {
			if ( p != null )
				p.close();
		}
	}

	public static List<Client> loadAll() throws FileNotFoundException {
		List<Client> list = new ArrayList<Client>();
		Scanner scan = new Scanner( new File( "client.db" ) );
		while ( scan.hasNext() ) {
			String[] str = scan.nextLine().split( "," );
			Client c = new Client( str[0], str[1] );
			c.setPhone( str[2] );
			c.setAdress( str[3] );
			list.add( c );
		}
		return list;
	}
}
